//
//  SpecialViewController.m
//  Coco_Music
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "SpecialViewController.h"

@interface SpecialViewController ()

@end

@implementation SpecialViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
         [self.myCollection reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    
    self.singC =[[SingerController alloc]init];
    self.similarVC =[[SimilarSingerViewController alloc]init];
    self.singMV =[[SingerMvViewController alloc]init];
    [self addChildViewController:self.singC];
    [self addChildViewController:self.similarVC];
    [self addChildViewController:self.singMV];
    [self.view addSubview:self.singC.view];
    [self.view addSubview:self.similarVC.view];
    [self.view addSubview:self.singMV.view];

    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载,请稍后";
    [self.HUD show:YES];

  
    
    [self getData];
}
-(void)createTable
{
    
    self.myView =[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3)];
    self.myView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.myView];
    [_myView release];
    self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3-40)];
    [self.myView addSubview:self.imageView];
    UIVisualEffectView *visuaIEffct =[[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visuaIEffct.frame = self.imageView.frame;
    visuaIEffct.alpha = 1;
    [self.myView addSubview:visuaIEffct];
    
    self.myImageView1 =[[UIImageView alloc]initWithFrame:CGRectMake(self.imageView.center.x-70, self.imageView.center.y-70 , 140, 140)];
    self.myImageView1.layer.masksToBounds = YES;
    self.myImageView1.layer.cornerRadius = 10;
    [visuaIEffct addSubview:self.myImageView1];
    
    
    
    
    [_myImageView1 release];
    
    
    [_imageView release];
    
    self.segmentV =[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"专辑",@"单曲",@"Mv",@"相似歌手", nil]];
    self.segmentV.frame = CGRectMake(0, self.imageView.frame.origin.y+self.imageView.frame.size.height-5,[UIScreen mainScreen].bounds.size.width, 45);
    
    [self.segmentV addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    self.segmentV.selectedSegmentIndex = 0;
    self.segmentV.backgroundColor =[UIColor whiteColor];
    self.segmentV.tintColor = [UIColor colorWithRed:244.0f/255.0f green:147.0f/255.0f blue:23.0f/255.0f alpha:1.0f];
    [_myView addSubview:self.segmentV];
    
    
    
    
    
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    //设置item大小
    flowLayout.itemSize= CGSizeMake(([UIScreen mainScreen].bounds.size.width-30)/2,([UIScreen mainScreen].bounds.size.width-30)/2);
    //底部区域大小
    flowLayout.footerReferenceSize = CGSizeMake(150,60);
    //滚动方向
    flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;
    //section距离边界的距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //每行内部cell item 的间距
    flowLayout.minimumInteritemSpacing = 10;;
    //每行内部cell item的间距
    flowLayout.minimumLineSpacing = 10;
    self.myCollection =[[UICollectionView alloc]initWithFrame:CGRectMake(0, self.segmentV.frame.size.height+self.segmentV.frame.origin.y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - (self.segmentV.frame.size.height+self.segmentV.frame.origin.y)-64) collectionViewLayout:flowLayout];
    self.myCollection.backgroundColor =[UIColor whiteColor];
    
    [self.view addSubview:self.myCollection];
    [_myCollection release];
    self.myCollection.delegate =self;
    self.myCollection.dataSource = self;
    //需要注册
    [self.myCollection registerClass:[SpecialCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.myCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerReuse"];
    [self.myCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    

}
-(void)bringSubView:(UIView *)myView
{
    [self.view addSubview:myView];
    [self.view addSubview:self.myView];
    
}
-(void)change:(UISegmentedControl *)sc
{
    if(sc.selectedSegmentIndex ==1){
        [self bringSubView:self.singC.view];
        self.singC.myTableView.tableHeaderView = self.myView;
        self.singC.SongID = self.SpecialID;
//        [self.singC.myTableView reloadData];
        
        
        
    }if(sc.selectedSegmentIndex ==2){
        [self bringSubView:self.singMV.view];
         self.singMV.SingerId=self.SpecialID;
        self.singMV.myTableView.tableHeaderView = self.myView;
//        [self.singMV.myTableView reloadData];
        
        
        
        
    }if (sc.selectedSegmentIndex ==3) {
        [self bringSubView:self.similarVC.view];
        self.similarVC.myTabView.tableHeaderView =self.myView;
        self.similarVC.SongID =self.SpecialID;
        self.similarVC.myDelegate =self;
    
    }
    
    if(sc.selectedSegmentIndex ==0){
        [self bringSubView:self.myCollection];
//        [self.myCollection reloadData];
        
    }
    
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView =[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerReuse" forIndexPath:indexPath];
        
        return footerView;
    }else
    {
        UICollectionReusableView *heartView=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        heartView.backgroundColor =[UIColor blueColor];
        return heartView;
    }
}
#define SpecialConnect @"http://api.dongting.com/song/singer/%ld/albums?"
#define SpecialConnect1 @"page=1&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=5df9002635a21cc5f7f660e71a16d1e35e5a04a0&idfa=B5F4461B-F9C7-4F1E-BA92-EB2E683311DF&utdid=VYEt7JjHCIUDAGowp1W3cSW3&alf=201200&bundle_id=com.ttpod.music"

-(void)getData
{
    NSString *url =[NSString stringWithFormat:SpecialConnect,self.SpecialID];

    
    NSString *url1 =[NSString stringWithFormat:url,SpecialConnect1];
    [AFN_block NSString:url1 Blokc:^(id request) {
        NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary:request];
        NSLog(@"专辑详情 %@",dic);
        NSMutableArray *dataArray =[dic objectForKey:@"data"];
        self.myArray =[NSMutableArray array];
        for (NSMutableDictionary *myDic in dataArray) {
            specila *Special =[[specila alloc]init];
            Special.albumId =[[myDic objectForKey:@"albumId"]integerValue];
            Special.singerId =[[myDic objectForKey:@"singerId"]integerValue];
            Special.song_name =[myDic objectForKey:@"name"];
            Special.singer_name =[myDic objectForKey:@"singerName"];
            Special.publishDate =[myDic objectForKey:@"publishDate"];
            Special.picUrl =[myDic objectForKey:@"picUrl"];
            Special.singerPicUrl =[myDic objectForKey:@"singerPicUrl"];
            
                    [self createTable];
            
            NSURL *url =[NSURL URLWithString:Special.singerPicUrl];
            [self.myImageView1 sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
            [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
            
            [self.myArray addObject:Special];
            [Special release];
    
            
            
        }
        [self.myCollection reloadData];
         [self.HUD hide:YES];
        
    }];
}
-(void)ChangeID:(NSInteger)ID
{
    
   
    self.SpecialID = ID;
    
 
    [self reloadData:ID];
    
    
    
}
-(void)changeSegmentIndex
{
    self.segmentV.selectedSegmentIndex = 0;
      [self bringSubView:self.myCollection];
}
-(void)reloadData:(NSInteger)ID
{
    NSString *url =[NSString stringWithFormat:SpecialConnect,ID];

    
    NSString *url1 =[NSString stringWithFormat:url,SpecialConnect1];
    [AFN_block NSString:url1 Blokc:^(id request) {
        NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary:request];
        NSMutableArray *dataArray =[dic objectForKey:@"data"];
        self.myArray =[NSMutableArray array];
        for (NSMutableDictionary *myDic in dataArray) {
            specila *Special =[[specila alloc]init];
            Special.albumId =[[myDic objectForKey:@"albumId"]integerValue];
            Special.singerId =[[myDic objectForKey:@"singerId"]integerValue];
            Special.song_name =[myDic objectForKey:@"name"];
            Special.singer_name =[myDic objectForKey:@"singerName"];
            Special.publishDate =[myDic objectForKey:@"publishDate"];
            Special.picUrl =[myDic objectForKey:@"picUrl"];
            Special.singerPicUrl =[myDic objectForKey:@"singerPicUrl"];
            
            NSURL *url =[NSURL URLWithString:Special.singerPicUrl ];
        
            [self.myImageView1 sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
            [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];

            [self.myArray addObject:Special];
            [Special release];
            
            
        }
        [self changeSegmentIndex];
        [self.myCollection reloadData];
        [self.HUD hide:YES];
        
    }];

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialDetailViewController *specialDVC =[[SpecialDetailViewController
                                               alloc]init];
    specialDVC.SingerID =[[self.myArray objectAtIndex:indexPath.row]albumId];
    [self.navigationController pushViewController:specialDVC animated:YES];
    
}
-(NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.mySpecial =[self.myArray objectAtIndex:indexPath.row];
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
        
    }];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
